package top.chengdongqing.mishop.utils

import top.chengdongqing.mishop.common.constants.RegexPatterns

/**
 * 数据脱敏工具类
 */
object DesensitizingUtils {

    fun desensitizeNickname(nickname: String): String {
        val firstChar = nickname[0]
        val lastChar = nickname[nickname.length - 1]
        val maskedMiddle = "*".repeat(maxOf(nickname.length - 2, 2))

        return "$firstChar$maskedMiddle$lastChar"
    }

    fun desensitizeAccount(account: String): String {
        var desensitizedData = account

        if (RegexPatterns.EMAIL.matches(account)) {
            // 处理电子邮件地址
            val parts = account.split("@")
            val username = parts[0]
            val domain = parts[1]
            val usernameLength = username.length
            val desensitizedUsername = "${username[0]}${"*".repeat(usernameLength - 2)}${username[usernameLength - 1]}"
            desensitizedData = "$desensitizedUsername@$domain"
        } else if (RegexPatterns.PHONE_NUMBER.matches(account)) {
            // 处理手机号码
            desensitizedData = "${account.substring(0, 3)}****${account.substring(7)}"
        }

        return desensitizedData
    }
}
